package com.hoopchina.ui.main;

import java.util.EmptyStackException;

import com.hoopchina.service.NewsService;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
/**
 * BaseActivity
 * 需要进行界面管理的Activity的基类
 * 
 * @author Neusoft
 *
 */
public abstract class BaseActivity extends Activity {
	public    static String               Id;
	protected ViewManager                 mViewManager = ViewManager.getInstance();
	protected boolean                     mMyHandleBackButton;
	private Handler mHandler = new Handler();
	
	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
	}
	/*
	 * 点击后列表跳到第一项
	 */
	public void clickToTop( ) {
		
	}
	
	@Override
    public void onResume() {
    	super.onResume();
    	Log.d("tt","MainActivity onResume() this = "+this);
    	mViewManager.setCurrentActivity( this );
//    	if ( mMyHandleBack ) {
//	    	iViewManager.setBackButton( iBackText, new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					goBack();
//				}
//	        });
//    	}
    }    
	/*
	 * 对点击返回键进行处理
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
  	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if( keyCode == KeyEvent.KEYCODE_BACK ) {
        	if ( mMyHandleBackButton ) {
	        	goBack();
	        	return true;
        	}
        	else {
        		return false;
        	}
        }
        else {
        	return super.onKeyDown(keyCode, event);
        }
    }
    
	/**
	 * mMyHandleBackButton属性为true时，点击返回键执行的方法
	 */
    protected void goBack() {
    	
        mHandler.postDelayed( new Runnable() {           
            @Override
            public void run() {
                if ( mViewManager.canGoBack() ) {
                    try {
                        mViewManager.goBack( );
                    }
                    catch ( EmptyStackException nsee ) {
                        nsee.printStackTrace();
                    }
                }
                else {
                    // shouldn't be here
                }
            }
        },
        400 );
    }
}
